package com.sztz.utils;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.merge.OnceAbsoluteMergeStrategy;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.sztz.excel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

/**
 * EasyExcel常用工具类
 * @author yzy
 * @date 2022/8/2 10:11
 */
public final class EasyExcelUtils {
    /**
     * 生成ExcelWriter写入器，带Head信息
     * @author yzy
     * @date 2022/8/02 10:30
     */
    public static ExcelWriter getExcelWriter(BufferedOutputStream outputStream, Class head) {
        return EasyExcelFactory.write(outputStream)
                .registerConverter(new LocalDateTimeConverter())
                .registerWriteHandler(EasyExcelUtils.getStyleStrategy())
                .registerWriteHandler(new AutoCellWidthStrategy())
                .head(head).build();
    }

    /**
     * 生成ExcelWriter构造器，自定义了单元格样式策略
     * @author yzy
     * @date 2023/01/07 10:30
     */
    public static ExcelWriterBuilder getExcelWriterByCellType(File file, Class head, List<String> cellDataTypes) {
        return EasyExcelFactory.write(file,head)
                .registerWriteHandler(new ExcelCellStyleStrategy(cellDataTypes));
    }
    /**
     * 生成ExcelWriter构造器，自定义了单元格样式策略
     * @author yzy
     * @date 2023/01/07 10:30
     */
    public static ExcelWriterBuilder getExcelWriterForCellFmt(File file, Class head, Map<String,List<Integer>> columnIndexMap, Map<String,List<String>> rowIndexMap) {
        return EasyExcelFactory.write(file,head)
                .registerWriteHandler(new CellStyleForFmtStrategy(columnIndexMap,rowIndexMap));
    }
    /**
     * 生成ExcelWriter写入器，不带头信息，有合并单元格规则
     * @author yzy
     * @date 2022/8/02 10:30
     */
    public static ExcelWriter getExcelWriter(BufferedOutputStream outputStream, OnceAbsoluteMergeStrategy onceAbsoluteMergeStrategy) {
        return EasyExcelFactory.write(outputStream)
                .registerConverter(new LocalDateTimeConverter())
                .registerWriteHandler(EasyExcelUtils.getStyleStrategy())
                .registerWriteHandler(new AutoCellWidthStrategy())
                .registerWriteHandler(onceAbsoluteMergeStrategy)
                .build();
    }
    /**
     * 生成WriteSheet, Excel页
     * @param sheetName 名称
     * @param sheetNo 页码
     * @author yzy
     * @date 2022/8/02 10:30
     */
    public static WriteSheet getWriteSheet(String sheetName,int sheetNo) {
        //自定义一下sheet页
        WriteSheet writeSheet = new WriteSheet();
        writeSheet.setSheetName(sheetName);
        writeSheet.setSheetNo(sheetNo);
        return writeSheet;
    }
    /**
     * 直接导出Excel到输出流
     * @author yzy
     * @date 2022/8/02 10:30
     */
    public static void export(OutputStream outputStream, Class head, List data, String sheetName, HorizontalCellStyleStrategy horizontalCellStyleStrategy) {
        //给定导出实体类
        EasyExcelFactory.write(outputStream, head)
                //给定工作表名称
                .sheet(sheetName)
                .registerConverter(new LocalDateTimeConverter())
                //给定样式
                .registerWriteHandler(horizontalCellStyleStrategy)
                //给定导出数据
                .doWrite(data);
    }
    /**
     * 设置请求头、文件名
     * @author yzy
     * @date 2022/8/02 10:13
     */
    public static void setResponse(HttpServletResponse response, String fileName) throws UnsupportedEncodingException {
        //编码设置成UTF-8，excel文件格式为.xlsx
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("UTF-8");
        // 这里URLEncoder.encode可以防止中文乱码 和easyexcel本身没有关系
        fileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.name());
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
    }
    /**
     * 设置生成excel样式 去除默认表头样式及设置内容居中
     * return HorizontalCellStyleStrategy
     * @author yzy
     * @date 2022/8/02 10:13
     */
    public static HorizontalCellStyleStrategy getStyleStrategy() {
        //内容样式策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        //垂直居中,水平居中
        contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        //设置不自动换行, 因为有自适应宽度的策略
        contentWriteCellStyle.setWrapped(Boolean.FALSE);
        // 字体策略
        WriteFont contentWriteFont = new WriteFont();
        // 字体大小
        contentWriteFont.setFontHeightInPoints((short) 12);
        contentWriteCellStyle.setWriteFont(contentWriteFont);
        //使用默认头策略
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        return new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
    }


}
